<?php

/**
 * Tiers form base class.
 *
 * @method Tiers getObject() Returns the current form's model object
 *
 * @package    symfony
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 29553 2010-05-20 14:33:00Z Kris.Wallsmith $
 */
abstract class BaseTiersForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                               => new sfWidgetFormInputHidden(),
      'code'                             => new sfWidgetFormInputText(),
      'id_nature_tiers'                  => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('NatureTiers'), 'add_empty' => true)),
      'matricule_fiscale'                => new sfWidgetFormInputText(),
      'raison_sociale'                   => new sfWidgetFormInputText(),
      'telephone1'                       => new sfWidgetFormInputText(),
      'telephone2'                       => new sfWidgetFormInputText(),
      'fax'                              => new sfWidgetFormInputText(),
      'email'                            => new sfWidgetFormInputText(),
      'site_web'                         => new sfWidgetFormInputText(),
      'code_douane'                      => new sfWidgetFormInputText(),
      'numero_exoneration_t_v_a'         => new sfWidgetFormInputText(),
      'numero_exoneration_timbre_fiscal' => new sfWidgetFormInputText(),
      'numero_exoneration_autre'         => new sfWidgetFormInputText(),
      'date_naissance'                   => new sfWidgetFormDate(),
      'date_changement_categorie_remise' => new sfWidgetFormDate(),
      'image'                            => new sfWidgetFormInputText(),
      'mise_en_sommeil'                  => new sfWidgetFormInputCheckbox(),
      'bloquer'                          => new sfWidgetFormInputCheckbox(),
      'majore'                           => new sfWidgetFormInputCheckbox(),
      'nombre_exemplaire_facture'        => new sfWidgetFormInputText(),
      'nombre_exemplaire_avoirs'         => new sfWidgetFormInputText(),
      'nombre_exemplaire_devis'          => new sfWidgetFormInputText(),
      'nombre_exemplaire_bon_commande'   => new sfWidgetFormInputText(),
      'nombre_exemplaire_bon_livraison'  => new sfWidgetFormInputText(),
      'nombre_exemplaire_bon_retour'     => new sfWidgetFormInputText(),
      'nombre_exemplaire_facture_avoir'  => new sfWidgetFormInputText(),
      'nombre_exemplaire_facture_retour' => new sfWidgetFormInputText(),
      'observation'                      => new sfWidgetFormInputText(),
      'type'                             => new sfWidgetFormInputText(),
      'solvabilite'                      => new sfWidgetFormInputCheckbox(),
      'GSM1'                             => new sfWidgetFormInputText(),
      'GSM2'                             => new sfWidgetFormInputText(),
      'numero_agrement'                  => new sfWidgetFormInputText(),
      'nom'                              => new sfWidgetFormInputText(),
      'prenom'                           => new sfWidgetFormInputText(),
      'fidele'                           => new sfWidgetFormInputCheckbox(),
      'ville_id'                         => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Ville'), 'add_empty' => false)),
      'id_forme_juridique'               => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('FormeJuridique'), 'add_empty' => false)),
      'id_nationalite'                   => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Nationalite'), 'add_empty' => true)),
      'civilite'                         => new sfWidgetFormChoice(array('choices' => array('M' => 'M', 'Mme' => 'Mme', 'Mlle' => 'Mlle'))),
      'id_categorie_comptable'           => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('CategorieComptable'), 'add_empty' => false)),
      'id_organisme'                     => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Organisme'), 'add_empty' => true)),
      'id_categorie_remise'              => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('CategorieRemise'), 'add_empty' => true)),
      'id_societe_conventionnee'         => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('SocieteConventionnee'), 'add_empty' => true)),
      'compte_comptable'                 => new sfWidgetFormInputText(),
      'credit_max'                       => new sfWidgetFormInputText(),
      'id_modalite_paiement'             => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('ModalitePaiement'), 'add_empty' => true)),
      'id_nature_societe'                => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('NatureSociete'), 'add_empty' => true)),
      'code_tiers'                       => new sfWidgetFormInputText(),
      'manager_id'                       => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Manager'), 'add_empty' => true)),
      'created_at'                       => new sfWidgetFormDateTime(),
      'updated_at'                       => new sfWidgetFormDateTime(),
      'tarifs_list'                      => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Tarif')),
    ));

    $this->setValidators(array(
      'id'                               => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
      'code'                             => new sfValidatorString(array('max_length' => 225)),
      'id_nature_tiers'                  => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('NatureTiers'), 'required' => false)),
      'matricule_fiscale'                => new sfValidatorString(array('max_length' => 225, 'required' => false)),
      'raison_sociale'                   => new sfValidatorString(array('max_length' => 225, 'required' => false)),
      'telephone1'                       => new sfValidatorString(array('max_length' => 225, 'required' => false)),
      'telephone2'                       => new sfValidatorString(array('max_length' => 225, 'required' => false)),
      'fax'                              => new sfValidatorString(array('max_length' => 225, 'required' => false)),
      'email'                            => new sfValidatorString(array('max_length' => 225, 'required' => false)),
      'site_web'                         => new sfValidatorString(array('max_length' => 225, 'required' => false)),
      'code_douane'                      => new sfValidatorString(array('max_length' => 225, 'required' => false)),
      'numero_exoneration_t_v_a'         => new sfValidatorString(array('max_length' => 225, 'required' => false)),
      'numero_exoneration_timbre_fiscal' => new sfValidatorString(array('max_length' => 225, 'required' => false)),
      'numero_exoneration_autre'         => new sfValidatorString(array('max_length' => 225, 'required' => false)),
      'date_naissance'                   => new sfValidatorDate(array('required' => false)),
      'date_changement_categorie_remise' => new sfValidatorDate(array('required' => false)),
      'image'                            => new sfValidatorString(array('max_length' => 225, 'required' => false)),
      'mise_en_sommeil'                  => new sfValidatorBoolean(array('required' => false)),
      'bloquer'                          => new sfValidatorBoolean(array('required' => false)),
      'majore'                           => new sfValidatorBoolean(array('required' => false)),
      'nombre_exemplaire_facture'        => new sfValidatorInteger(array('required' => false)),
      'nombre_exemplaire_avoirs'         => new sfValidatorInteger(array('required' => false)),
      'nombre_exemplaire_devis'          => new sfValidatorInteger(array('required' => false)),
      'nombre_exemplaire_bon_commande'   => new sfValidatorInteger(array('required' => false)),
      'nombre_exemplaire_bon_livraison'  => new sfValidatorInteger(array('required' => false)),
      'nombre_exemplaire_bon_retour'     => new sfValidatorInteger(array('required' => false)),
      'nombre_exemplaire_facture_avoir'  => new sfValidatorInteger(array('required' => false)),
      'nombre_exemplaire_facture_retour' => new sfValidatorInteger(array('required' => false)),
      'observation'                      => new sfValidatorString(array('max_length' => 225, 'required' => false)),
      'type'                             => new sfValidatorInteger(array('required' => false)),
      'solvabilite'                      => new sfValidatorBoolean(array('required' => false)),
      'GSM1'                             => new sfValidatorString(array('max_length' => 32, 'required' => false)),
      'GSM2'                             => new sfValidatorString(array('max_length' => 32, 'required' => false)),
      'numero_agrement'                  => new sfValidatorNumber(array('required' => false)),
      'nom'                              => new sfValidatorString(array('max_length' => 225, 'required' => false)),
      'prenom'                           => new sfValidatorString(array('max_length' => 225, 'required' => false)),
      'fidele'                           => new sfValidatorBoolean(array('required' => false)),
      'ville_id'                         => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Ville'))),
      'id_forme_juridique'               => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('FormeJuridique'), 'required' => false)),
      'id_nationalite'                   => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Nationalite'), 'required' => false)),
      'civilite'                         => new sfValidatorChoice(array('choices' => array(0 => 'M', 1 => 'Mme', 2 => 'Mlle'), 'required' => false)),
      'id_categorie_comptable'           => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('CategorieComptable'))),
      'id_organisme'                     => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Organisme'), 'required' => false)),
      'id_categorie_remise'              => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('CategorieRemise'), 'required' => false)),
      'id_societe_conventionnee'         => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('SocieteConventionnee'), 'required' => false)),
      'compte_comptable'                 => new sfValidatorString(array('max_length' => 10, 'required' => false)),
      'credit_max'                       => new sfValidatorNumber(array('required' => false)),
      'id_modalite_paiement'             => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('ModalitePaiement'), 'required' => false)),
      'id_nature_societe'                => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('NatureSociete'), 'required' => false)),
      'code_tiers'                       => new sfValidatorInteger(array('required' => false)),
      'manager_id'                       => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Manager'), 'required' => false)),
      'created_at'                       => new sfValidatorDateTime(),
      'updated_at'                       => new sfValidatorDateTime(),
      'tarifs_list'                      => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Tarif', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('tiers[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'Tiers';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['tarifs_list']))
    {
      $this->setDefault('tarifs_list', $this->object->Tarifs->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->saveTarifsList($con);

    parent::doSave($con);
  }

  public function saveTarifsList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['tarifs_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Tarifs->getPrimaryKeys();
    $values = $this->getValue('tarifs_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Tarifs', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Tarifs', array_values($link));
    }
  }

}
